﻿using ComponentGIS.Properties;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System;
using System.IO;
using System.Windows.Forms;

namespace ComponentGIS.Tool
{
    class DelVertexTool : ICommand, ITool
    {
        #region 字段属性
        private IHookHelper m_pHookHelper;
        private IEngineEditor m_pEditor;
        private IEngineEditSketch m_pSketch;
        private Cursor m_DeleteVertexCursor;
        #endregion

        #region ITool成员
        public int Cursor
        {
            get
            {
                m_DeleteVertexCursor = new Cursor(new MemoryStream(Resources.DeleteVertexCursor));
                return m_DeleteVertexCursor.Handle.ToInt32();
                //ISystemMouseCursor pMouse = new SystemMouseCursorClass();
                //pMouse.Load(esriSystemMouseCursor.esriSystemMouseCursorArrow);
                //return pMouse.Cursor;
            }
        }

        public bool Deactivate()
        {
            return true;
        }

        public bool OnContextMenu(int x, int y)
        {
            return false;
        }

        public void OnDblClick()
        {
        }

        public void OnKeyDown(int keyCode, int shift)
        {
        }

        public void OnKeyUp(int keyCode, int shift)
        {
        }

        public void OnMouseDown(int button, int shift, int x, int y)
        {
            //转成地图坐标
            IPoint clickedPt = m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
            if (button == 1)
            {
                #region 1 获取点击测试参数
                IHitTest hitShape = (IHitTest)m_pSketch.Geometry;
                IPoint hitPoint = new PointClass();
                double hitDistance = 0;
                int hitPartIndex = 0;
                int hitSegmentIndex = 0;
                bool bRightSide = false;
                esriGeometryHitPartType hitPartType = esriGeometryHitPartType.esriGeometryPartNone;
                double searchRadius = 50;
                #endregion

                //2 节点判断
                hitPartType = esriGeometryHitPartType.esriGeometryPartVertex;
                bool isTrue = hitShape.HitTest(clickedPt, searchRadius, hitPartType, hitPoint, ref hitDistance,
                     ref hitPartIndex, ref hitSegmentIndex, ref bRightSide);
                //3 删除节点 
                if (isTrue)
                {
                    //3.1 草图操作开始
                    IEngineSketchOperation pSketchOp = new EngineSketchOperationClass();
                    pSketchOp.Start(m_pEditor);
                    pSketchOp.SetMenuString("Delete Vertex (Custom)");
                    //3.2 获取点串
                    IGeometryCollection pGeoCol = (IGeometryCollection)m_pSketch.Geometry;
                    IPointCollection pPathOrRingPtCol = (IPointCollection)pGeoCol.get_Geometry(hitPartIndex);
                    //3.3 删除节点
                    object missing = Type.Missing;
                    object partIndexObject = hitPartIndex;
                    pPathOrRingPtCol.RemovePoints(hitSegmentIndex, 1);
                    //4.4 移除旧的，添加新的
                    pGeoCol.RemoveGeometries(hitPartIndex, 1);
                    pGeoCol.AddGeometry((IGeometry)pPathOrRingPtCol, ref partIndexObject, ref missing);
                    //4.5 草图操作完成
                    esriEngineSketchOperationType opType =
                        esriEngineSketchOperationType.esriEngineSketchOperationVertexDeleted;
                    pSketchOp.Finish(null, opType, hitPoint);
                }
            }
        }

        public void OnMouseMove(int button, int shift, int x, int y)
        {
        }

        public void OnMouseUp(int button, int shift, int x, int y)
        {

        }

        public void Refresh(int hdc)
        {
        }
        #endregion

        #region ICommand成员
        public int Bitmap
        {
            get { return -1; }
        }

        public string Caption
        {
            get { return "删除节点"; }
        }

        public string Category
        {
            get { return "空间编辑"; }
        }

        public bool Checked
        {
            get { return false; }
        }

        public bool Enabled
        {
            get { return true; }
        }

        public int HelpContextID
        {
            get { return -1; }
        }

        public string HelpFile
        {
            get { return string.Empty; }
        }

        public string Message
        {
            get { return "删除节点"; }
        }

        public string Name
        {
            get { return "DelVertexTool"; }
        }

        public void OnClick()
        {

        }

        public void OnCreate(object Hook)
        {
            m_pHookHelper = new HookHelperClass();
            m_pHookHelper.Hook = Hook;
            m_pEditor = new EngineEditorClass();
            m_pSketch = (IEngineEditSketch)m_pEditor;
        }

        public string Tooltip
        {
            get { return "删除节点"; }
        }
        #endregion
    }
}
